package com.jq.jqaicode.service.impl;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jq.jqaicode.mapper.UserMapper;
import com.jq.jqaicode.model.entity.User;
import com.jq.jqaicode.service.UserService;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {


    @Override
    public SaTokenInfo doLogin(String username, String password) {
        User user = this.getOne(new QueryWrapper<User>().eq("user_no", username).eq("pwd", password));
        if(user!=null){
            user.setLoginTime(new Date());
            this.updateById(user);
            // 第1步，先登录上
            StpUtil.login(user.getId());
            // 第2步，获取 Token  相关参数
            SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
            return tokenInfo;
        }
        return null;
    }

    @Override
    public User getUserInfo(String id) {
        return this.getById(id);
    }


}
